<template>
  <div class="app-container">
    <el-form class="formWidth" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="信件类型" prop="typeId">
        <el-select v-model="queryParams.typeId" placeholder="请选择信件类型" clearable size="small">
          <el-option
            v-for="dict in typeIdOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="信访主题" prop="theme">
        <el-input
          v-model="queryParams.theme"
          placeholder="请输入信访主题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择处理状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="信访时间" prop="petitionTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.petitionTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择信访时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['comprehensive:petitionLetter:add']"-->
<!--        >新增</el-button>-->
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['comprehensive:petitionLetter:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['comprehensive:petitionLetter:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['comprehensive:petitionLetter:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="petitionLetterList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->

      <el-table-column label="信件类型" :show-overflow-tooltip="true" align="center" prop="typeId" :formatter="typeIdFormat" />
      <el-table-column label="信访主题" :show-overflow-tooltip="true" align="center" prop="theme" />
      <el-table-column label="信访内容" :show-overflow-tooltip="true" align="center" prop="content" :formatter="removeTAG" />
      <el-table-column label="处理状态" :show-overflow-tooltip="true" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="信访时间" :show-overflow-tooltip="true" align="center" prop="petitionTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.petitionTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleView(scope.row)"
            v-hasPermi="['comprehensive:petitionLetter:edit']"
          >查看回复</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleEnd(scope.row)"
            v-hasPermi="['comprehensive:petitionLetter:remove']"
          >完结</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改网上信访对话框 -->
    <el-dialog class="formWidth" :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="信件类型" prop="typeId">
          <el-select v-model="form.typeId" placeholder="请选择信件类型">
            <el-option
              v-for="dict in typeIdOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="信访主题" prop="theme">
          <el-input v-model="form.theme" placeholder="请输入信访主题" />
        </el-form-item>
        <el-form-item label="信访内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看回复弹框 开始 -->
    <el-dialog class="formWidth"   title="查看回复" :visible.sync="openReply" width="900px" append-to-body>
      <div v-loading="detailsLoading">
        <div class="content">
          <div class="box">
            <div class="title1">信访内容：</div>
            <div class="replyContent" v-html="replyAllInfo.content"></div>
          </div>
        </div>
        <div class="reply">
          <div class="title1">回复历史：</div>


<!--          <div class="replyHistory">-->
<!--            <div class="replyitem" v-for="(item,i) in replyAllInfo.processList" :key="i" v-bind:class="{ 'right': item.handleUser==1, 'left': item.handleUser==0 }" >-->
<!--              {{item.handleContent}}-->
<!--              <div class="time">{{item.handleTime}}</div>-->
<!--            </div>-->
<!--          </div>-->

          <div class="newReplyHistory">
            <div class="item" v-for="(item,i) in replyAllInfo.processList" :key="i">
              <div class="head"  v-bind:class="{ 'active': item.handleUser==1, 'normal': item.handleUser==0 }">
                <div class="line"></div>
                <div class="user">{{item.handleUser==1?'系统管理员':'用户'}}</div>
                <div class="time">{{item.handleTime}}</div>
              </div>
              <div class="content">
                {{item.handleContent}}
              </div>
            </div>
          </div>

          <div class="replyInput">
            <el-input
              type="textarea"
              clearable
              placeholder="请输入回复内容"
              v-model="replyContent"
              maxlength="500"
              show-word-limit/>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="replyLoading" :disabled="rowInfo.status == 3" type="primary" @click="submitReply">回复</el-button>
        <el-button @click="openReply = !openReply ">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 查看回复弹框 结束 -->

  </div>
</template>

<script>
import {submitReply, listPetitionLetter, end, getPetitionLetter, delPetitionLetter, addPetitionLetter, updatePetitionLetter, exportPetitionLetter } from "@/api/comprehensive/petitionLetter";

export default {
  name: "PetitionLetter",
  data() {
    return {
      // 回复
      openReply:false,
      replyContent:'',
      replyAllInfo:'',
      replyLoading:false,
      detailsLoading:false,
      rowInfo:"",

      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 网上信访表格数据
      petitionLetterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 信件类型字典
      typeIdOptions: [],
      // 处理状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        typeId: null,
        theme: null,
        status: null,
        petitionTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        typeId: [
          { required: true, message: "信件类型不能为空", trigger: "change" }
        ],
        theme: [
          { required: true, message: "信访主题不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "信访内容不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "处理状态不能为空", trigger: "change" }
        ],
      },

    };
  },
  created() {
    this.getList();
    this.getDicts("petition_type").then(response => {
      this.typeIdOptions = response.data;
    });
    this.getDicts("detail_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    removeTAG(row){
      let str = row.content;
      return str.replace(/<[^>]+>/g, "");
    },
  /** 查询网上信访列表 */
    getList() {
      this.loading = true;
      listPetitionLetter(this.queryParams).then(response => {
        this.petitionLetterList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 信件类型字典翻译
    typeIdFormat(row, column) {
      return this.selectDictLabel(this.typeIdOptions, row.typeId);
    },
    // 处理状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        soldierId: null,
        typeId: null,
        theme: null,
        content: null,
        status: null,
        petitionTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加网上信访";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePetitionLetter(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPetitionLetter(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有网上信访数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportPetitionLetter(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },


    /** 完结按钮操作 */
    handleEnd(row) {
      this.$confirm('是否确认完结该项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {

        end(row.id).then(res=>{
          this.msgSuccess("操作成功！")
          this.getList();
        }).catch(err=>{})

      }).catch(() => {});
    },

    /** 查看回复按钮操作 */
    handleView(row) {
      this.rowInfo = row;
      this.detailsLoading = true;
      this.openReply = true;
      // 后端去查详情
      getPetitionLetter(row.id).then(res=>{
        this.detailsLoading = false;
        this.replyAllInfo = res.data;
      }).catch(err=>{
        this.detailsLoading = false;
      })

    },

    /** 提交回复信息*/
    submitReply(){
      this.replyLoading=true;
      let params={
        petitionLetterId:this.replyAllInfo.id,
        handleContent:this.replyContent
      };
      submitReply(params).then(res=>{
        this.replyContent = '';
        this.replyLoading=false;
        this.msgSuccess("回复成功");
        this.openReply=false;
        this.detailsLoading = false;
      }).catch(err=>{
        this.replyLoading=false;
      })
    }


  }
};
</script>

<style lang="scss" scoped>
.content{

}
.reply{
  .title1{
    width: 100%;
    font-size: 16px;
    font-weight: bold;
    line-height: 25px;
  }
  margin-top: 15px;
  .replyHistory{
    width: 100%;
    height: 200px;
    overflow-y: auto;
    .replyitem{
      padding: 5px;
      margin-top: 15px;
      //border: 1px solid #cecece;
      border-radius: 5px;
      max-width: 600px;
      width: auto;
      height: auto;
      font-size: 15px;
      .time{
        font-size: 13px;
        color: #ddd;
      }
    }
    .right{
      margin-left: 240px;
      .time{
        text-align: right;
      }
    }
    .left{
      margin-right: 240px;
      .time{
        text-align: right;
      }
    }
  }
  .replyInput{
    margin-top: 15px;
    width: 100%;
    height: 50px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
  }

}

.head{
  font-size: 14px;
  font-weight: bold;
  margin-bottom: 15px;
  text-align: left;
}
.box{
  .title1{
    width: 100%;
    font-size: 16px;
    font-weight: bold;
    line-height: 25px;
  }

  .replyContent{
    width: 100%;
    line-height: 25px;
    font-size: 14px;
  }
}

.newReplyHistory{
  width: 100%;
  height: 300px;
  overflow-y: auto;
  .item{
    margin-bottom: 8px;
    .head{
      display: flex;
      width: 100%;
      height: 30px;
      justify-content: flex-start;
      align-items: flex-end;
      margin-bottom: 8px;
      .line{
        width: 15px;
        height: 15px;
        background: #333;
      }
      .user{
        font-size: 13px;
        font-weight: bold;
        margin: 0 15px;
      }
      .time{
        color: #ddd;
        font-size: 12px;
      }
    }
    .active{
      color:#00afff;
      .line{
        background: #00afff;
      }
    }
    .normal{
      color: #333;
      .line{
        background: #333;
      }
    }
    .content{
      font-size: 12px;
      color: #333;
    }
  }
}

</style>
